package controllers;

import java.util.List;

import javax.persistence.Query;

import models.Dept;
import models.Role;
import models.User;
import play.db.jpa.JPA;
import play.mvc.Before;
import play.mvc.Controller;
import utils.YUtils;

/**
 * Manage Users related operations.
 */

public class Users extends Controller {
	
    public static void index() {
    	List<Dept> depts = Dept.findAll();
    	List<Role> roles = Role.findAll();
        render(depts,roles);
    }

    /**
     * Get Users json.
     */
    public static void json(int page, int pagesize) {
    	
    	List<User> list = User.all().fetch(page, pagesize);      	 
    	long total = User.count();
    	
    	String notesStr = User.toCheckJson(list);
    	String jsonStr = YUtils.ligerGridData(notesStr, total);
    	
    	renderJSON(jsonStr);
    }
    /**
     * Update a User.
     */
    public static void save(User user,Long[] roleId) {
    	if(user.id==null){
    		user.initPassword();
    	}
    	user.save();
    	user.initRole(roleId);
        ok();
    }
    
    /**
     * Delete a User.
     */
    public static void delete(long id) {
        User.findById(id)._delete();
        ok();
    }

}
